package top.tupobi.zing.network.beiyong;


import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import top.tupobi.zing.network.APIFunction;
import top.tupobi.zing.network.HttpConfig;
import top.tupobi.zing.network.NetInterceptor;


public class RetrofitManagerBeiyong {

    private top.tupobi.zing.network.APIFunction APIFunction;
    private static boolean sIsSupportStringResponse;
    private static String sBaseURL;
    private static OkHttpClient sOkHttpClient;

    public static void initOkHttpClient() {
        sOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(HttpConfig
                        .HTTP_TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(HttpConfig.HTTP_TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(HttpConfig.HTTP_TIME_OUT, TimeUnit.SECONDS)
                .addInterceptor(NetInterceptor.getHeaderInterceptor())
                .addInterceptor(NetInterceptor.getLoggingInterceptor())//添加日志拦截器
//                .addNetworkInterceptor(new Interceptor() {
//                    @Override
//                    public Response intercept(@Nullable Chain chain) throws IOException {
//                        return null;
//                    }
//                })
                .build();
    }

    private RetrofitManagerBeiyong() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HttpConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())//添加gson转换器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
                .client(sOkHttpClient).build();
        APIFunction = retrofit.create(APIFunction.class);
    }

    private RetrofitManagerBeiyong(String baseURL) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseURL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(sOkHttpClient).build();
        APIFunction = retrofit.create(APIFunction.class);
    }

    private RetrofitManagerBeiyong(boolean isSupportStringResponse) {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(HttpConfig.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
        if (isSupportStringResponse) {
            builder.addConverterFactory(ScalarsConverterFactory.create());
        } else {
            builder.addConverterFactory(GsonConverterFactory.create());
        }
        Retrofit retrofit = builder.client(sOkHttpClient).build();
        APIFunction = retrofit.create(APIFunction.class);
    }

    private RetrofitManagerBeiyong(boolean isSupportStringResponse, String baseURL) {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(baseURL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
//        if (isSupportStringResponse) {
//            builder.addConverterFactory(ScalarsConverterFactory.create());
//        } else {
            builder.addConverterFactory(GsonConverterFactory.create());
//        }
        Retrofit retrofit = builder.client(sOkHttpClient).build();
        APIFunction = retrofit.create(APIFunction.class);
    }


    public APIFunction API() {
        return APIFunction;
    }

    /**
     * 静态单例，线程安全
     */
    private static class InstanceHolder1 {
        private static RetrofitManagerBeiyong sRetrofitManager = new RetrofitManagerBeiyong();
    }

    /**
     * 静态单例，线程安全
     */
    private static class InstanceHolder2 {
        private static RetrofitManagerBeiyong sRetrofitManager = new RetrofitManagerBeiyong(sBaseURL);
    }

    /**
     * 静态单例，线程安全
     */
    private static class InstanceHolder3 {
        private static RetrofitManagerBeiyong sRetrofitManager = new RetrofitManagerBeiyong(sIsSupportStringResponse);
    }

    /**
     * 静态单例，线程安全
     */
    private static class InstanceHolder4 {
        private static RetrofitManagerBeiyong sRetrofitManager = new RetrofitManagerBeiyong(sIsSupportStringResponse, sBaseURL);
    }

    /**
     * baseURL 默认
     *
     * @return 实例
     */
    public static RetrofitManagerBeiyong getInstance() {
        sBaseURL = HttpConfig.BASE_URL;
        sIsSupportStringResponse = false;
        return InstanceHolder1.sRetrofitManager;
    }

    public static RetrofitManagerBeiyong getInstance(String baseURL) {
        sBaseURL = baseURL;
        sIsSupportStringResponse = false;
        return InstanceHolder2.sRetrofitManager;
    }

    /**
     * baseURL 默认
     *
     * @param isSupportStringResponse 是否可返回String类型的返回体(GSON Scala解析器)
     * @return 实例
     */
    public static RetrofitManagerBeiyong getInstance(boolean isSupportStringResponse) {
        sBaseURL = HttpConfig.BASE_URL;
        sIsSupportStringResponse = isSupportStringResponse;
        return InstanceHolder3.sRetrofitManager;
    }

    /**
     * @param isSupportStringResponse 是否可返回String类型的返回体(GSON Scala解析器)
     * @param baseURL                 指定baseURL
     * @return 实例
     */
    public static RetrofitManagerBeiyong getInstance(boolean isSupportStringResponse, String baseURL) {
        sBaseURL = baseURL;
        sIsSupportStringResponse = isSupportStringResponse;
        return InstanceHolder4.sRetrofitManager;
    }


}
